$(document).ready(function () {
    var map = initialMap(function() {
        var point = new BMap.Point($("#xpos").val(), $("#ypos").val());
        map.centerAndZoom(point, 15);
        map.clearOverlays();
        var marker = new BMap.Marker(point);
        map.addOverlay(marker);
    });
    var geoc = new BMap.Geocoder();
    map.addEventListener("click", function(e){
        map.clearOverlays();
        var marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
        map.addOverlay(marker);
        $("#xpos").val(e.point.lng);
        $("#ypos").val(e.point.lat);
        geoc.getLocation(e.point, function(rs){
            var addComp = rs.addressComponents;
            $("#location").val((addComp.province + (addComp.city==addComp.province?"":addComp.city) + addComp.district + addComp.street + addComp.streetNumber));
        });
    });

    $(document).on("click", ".shop-detail", function(){
        $("#shop-detail-dialog").modal("show");
        loadShopDetail($(this).data("id"));
    });

    $(document).on("click", ".service-stop", function () {
        $("#service-stop-dialog").modal("show");
        $("#service-stop-confirm-btn").attr("data-id", $(this).attr("data-id"));
    });

    $(document).on("click", "#service-stop-confirm-btn", function () {
        stopService($(this).attr("data-id"));
    });

    $(document).on("click", "#modify-shop-info-btn", modifiyShopDetail);

    $(document).on("click", "#add-service-btn", function() {
        $("#modify-service-info-btn").unbind();
        $("#modify-service-info-btn").click(addServiceDetail);
        $("#service-detail-dialog").modal("show");
        loadServiceAdd($(this).data("shop-id"));
    });

    $(document).on("click", ".service-detail", function() {
        $("#modify-service-info-btn").unbind();
        $("#modify-service-info-btn").click(modifyServiceDetail);
        $("#service-detail-dialog").modal("show");
        loadServiceDetail($(this).data("id"));
    });

});

function loadShopDetail(id) {
    Util.ajax({
        type: "POST",
        url: "./rest/admin/shop/" + id,
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            var render = _.template($("#shop-detail-template").html());
            $("#shop-detail").html(render(data.data.shop));

            render = _.template($("#shop-service-template").html());
            var str = "";
            _.each(data.data.services, function (d) {
                str += render(d);
            });
            $("#services-detail").html(str);
        }
    });
}

function modifiyShopDetail() {
    if ($("#name").val()=="") {
        Util.errorTip("商家名称不能为空");
        return;
    }

    Util.ajax({
        type: "POST",
        url: "./rest/admin/shop/modify",
        data: $("#shop-detail-dialog form").serialize(),
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            location.reload();

            return "修改成功";
        }
    });
}

function loadServiceDetail(id) {
    Util.ajax({
        type: "POST",
        url: "./rest/admin/service/" + id,
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            showServiceDetail(data.data);

            return "加载成功";
        }
    });
}

function showServiceDetail(data) {
    var render = _.template($("#service-detail-template").html());
    $("#service-detail").html(render(data.service));

    render = _.template($("#service-type-template").html());
    var str = "";
    _.each(data.type, function (d) {
        str += render(d);
    });
    $("#service-type").html(str);

    $("#service-type").val(data.service.type.id);
}

function modifyServiceDetail() {
    if ($("#tag1")=="") {
        Util.errorTip("服务名称不能为空");
        return;
    }

    Util.ajax({
        type: "POST",
        url: "./rest/admin/service/modify",
        data: $("#service-detail-dialog form").serialize(),
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            loadShopDetail(data.data.shopinfo.id);

            $("#service-detail-dialog").modal("hide");

            return "修改成功";
        }
    });
}

function loadServiceAdd(shopid) {
    Util.ajax({
        type: "POST",
        url: "./rest/admin/service/type/list",
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            var json = {
                service : {
                    id: -1,
                    shopinfo: {id: shopid},
                    tag1: "",
                    creditcost: "",
                    type: {id : 0}
                },
                type : data.data
            };

            showServiceDetail(json);

            return "加载成功";
        }
    });
}

function addServiceDetail() {
    if ($("#tag1")=="") {
        Util.errorTip("服务名称不能为空");
        return;
    }

    Util.ajax({
        type: "POST",
        url: "./rest/admin/service/add",
        data: $("#service-detail-dialog form").serialize(),
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            var render = _.template($("#shop-service-template").html());
            $("#services-detail").append(render(data.data));

            $("#service-detail-dialog").modal("hide");

            return "添加成功";
        }
    });
}

function stopService(id) {
    Util.ajax({
        type: "POST",
        url: "./rest/admin/service/stop",
        data: {id: id},
        success: function (data) {
            if (data.status != 0) {
                return Util.errorData("信息获取失败");
            }

            loadShopDetail(data.data.shopinfo.id);

            return "添加成功";
        }
    });

}